#include "classic.h"
#include "ui_classic.h"
#include<QDebug>
Classic::Classic(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Classic)
{
    ui->setupUi(this);
    t="";
    wid=450;
    isPathfinding=false;
    isPosition=false;
    isHide=false;
    star=false;
    success=false;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    time.setHMS(0,0,0,0);//设置初始值
    timer = new QTimer(this);//创建一个定时器
    timer->setInterval(1000);// 设置超时间隔
    connect(timer, SIGNAL(timeout()),this,SLOT(showTime()));
}


Classic::~Classic()
{
    delete ui;
}
Node Classic::getStartPoint(){
    return this->startPoint;
}
void Classic::setStartPoint(Node node){
    this->startPoint=node;
}
bool Classic::isEven(int sum){
    if(sum%2){
        return false;
    }else{
        return true;
    }
}

void Classic::on_Return_clicked()
{
    timer->stop();
    emit ret();
}

void Classic::on_Button_1_clicked()
{
   t+="1";
   ui->textEdit->setText(t);
}

void Classic::on_Button_2_clicked()
{
    t+="2";
    ui->textEdit->setText(t);
}

void Classic::on_Button_3_clicked()
{
    t+="3";
    ui->textEdit->setText(t);
}

void Classic::on_Button_4_clicked()
{
    t+="4";
    ui->textEdit->setText(t);
}

void Classic::on_Button_5_clicked()
{
    t+="5";
    ui->textEdit->setText(t);
}

void Classic::on_Button_6_clicked()
{
    t+="6";
    ui->textEdit->setText(t);
}


void Classic::on_Button_7_clicked()
{
    t+="7";
    ui->textEdit->setText(t);
}

void Classic::on_Button_8_clicked()
{
    t+="8";
     ui->textEdit->setText(t);
}

void Classic::on_Button_9_clicked()
{
    t+="9";
    ui->textEdit->setText(t);
}

void Classic::on_Button_0_clicked()
{
    t+="0";
    ui->textEdit->setText(t);
}

void Classic::on_Cancle_clicked()
{
   t= t.mid(0,t.size()-1);
    ui->textEdit->setText(t);
}

void Classic::on_Confirm_clicked()
{

    if(ui->textEdit->toPlainText()!=""){
        isHide=true;
        n=ui->textEdit->toPlainText().toInt();
        ui->textEdit->setText("");
        t="";
        ui->label->setText(QString("迷宫大小：%1*%2").arg(n).arg(n));
        if(!isEven(n)){
            n++;
        }
        if(n<6){
            n=6;
            ui->label->setText("已超过迷宫最小生成范围，自动生成迷宫大小：6*6");
        }
        if(n>190){
            n=190;
            ui->label->setText("已超过迷宫最大生成范围，自动生成迷宫大小：190*190");
        }
        ui->promt->setText("Have a good day!");
        wid=1500/(n+4);
        maze.init(n);
        maze.mapBuid();
        startPoint=maze.map[2][2];

        success=false;
        time.setHMS(0,0,0,0);//设置初始值
        ui->time->setText(time.toString("hh:mm:ss"));
        timer->start();
        star=true;
        update();
    }
}
//显示时间
void Classic::showTime(){
    time=time.addSecs(1);
    ui->time->setText(time.toString("hh:mm:ss"));
}
//随机生成地图
void Classic::on_pushButton_clicked()
{

    isHide=true;
    n=(qrand()%45+6)*2;
    ui->label->setText(QString("迷宫大小：%1*%2").arg(n).arg(n));
    ui->promt->setText("Have a good day!");
    wid=1500/(n+4);
    maze.init(n);
    maze.mapBuid();
    startPoint=maze.map[2][2];
    success=false;
    time.setHMS(0,0,0,0);//设置初始值
    ui->time->setText(time.toString("hh:mm:ss"));
    timer->start();
    star=true;
    update();

}

//重写paintEvent函数
void Classic::paintEvent(QPaintEvent *)
{
    if(star){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/rec/260.png");
    //设置反锯齿模式
     painter.setRenderHint(QPainter::Antialiasing, true);
     if(success){
         pixmap.load(":/rec/success.jpeg");
         painter.drawPixmap(0, 0,1300 ,1300 ,pixmap);
     }else{
         for(int i=0;i<n+3;i++){
             for(int j=0;j<n+3;j++){
                 if(maze.map[i][j].flag==-1||maze.map[i][j].flag==0){
                     //墙
                     pixmap.load(":/rec/wall.jpeg");
                     painter.drawPixmap(i*wid, j*wid, wid, wid,pixmap);
                 }else{
                    painter.setBrush(QColor(255, 255, 255));
                    painter.drawRect(i*wid, j*wid, wid, wid);
                 }
             }
         }
         pixmap.load(":/rec/home.jpeg");
         painter.drawPixmap(2*wid, 2*wid, wid, wid,pixmap);
         pixmap.load(":/rec/end.jpg");
         painter.drawPixmap(n*wid, n*wid, wid, wid,pixmap);
         pixmap.load(":/rec/260.png");
         //寻路绘图
         if(isPathfinding){
              painter.setBrush(QColor(0, 255, 0));
              QList<Node> L=maze.getRoute();
              for(int i=1;i<L.size()-1;i++){
                      painter.drawRect(L[i].x*wid, L[i].y*wid, wid, wid);
              }
              painter.drawPixmap(startPoint.x*wid, startPoint.y*wid, wid, wid,pixmap);
              isPathfinding=false;
         }
         if(isPosition){
             painter.drawPixmap(startPoint.x*wid, startPoint.y*wid, wid, wid,pixmap);
             isPosition=false;
         }

     }


}
}

//寻路
void Classic::on_Auto_clicked()
{
    if(star){
        //初始化列表
        maze.setB(true);
        maze.setStEdP(startPoint,maze.map[n][n]);
        maze.mapSearch();
        isPathfinding=true;
        update();

    }
}
//按键操作
void Classic::keyPressEvent(QKeyEvent *e){
    //向上移动
    if(e->key()==0x57||e->key()==0x01000013){
        if(maze.map[startPoint.x][startPoint.y-1].flag==1){
            startPoint.y--;
        }

    }
    //向右移动
    if(e->key()==0x44 ||e->key()==0x01000014){
        if(maze.map[startPoint.x+1][startPoint.y].flag==1){
            startPoint.x++;
        }
    }
    //向下移动
    if(e->key()==0x53 ||e->key()==0x01000015){
        if(maze.map[startPoint.x][startPoint.y+1].flag==1){
            startPoint.y++;
        }
    }
    //向左移动
    if(e->key()== 0x41 ||e->key()==0x01000012){
        if(maze.map[startPoint.x-1][startPoint.y].flag==1){
            startPoint.x--;
        }
    }
    if(startPoint.x==n &&startPoint.y==n){
        success=true;
        timer->stop();
    }else{
        success=false;
    }
    isPosition=true;
    update();

}

